﻿<%@ Page Language="C#" MasterPageFile="~/masterpagecuatui.Master" AutoEventWireup="true" CodeBehind="Task.aspx.cs" Inherits="tracnghiemonline2.Task" %>

<%@ Import Namespace="DataAccess" %>
<%@ Import Namespace="System.Linq" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title><%=title %> - Tiếng nhật thật dễ</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="w3-row page-main-content w3-light-grey">
        <div class="w3-col l9 w3-padding">
            <div class="w3-row w3-teal w3-padding w3-hide-small">
                <%
                    var get_test = new TblTestController().FetchAll().Where(x => x.TestId == this.test).SingleOrDefault();
                    var get_cer = new TblCertificateController().FetchAll().Where(x => x.CerId == get_test.TestCertificate).SingleOrDefault();
                    string name = "Khách";
                    string cls = "...";
                    if (Session["user"] != null)
                    {
                        var get_name = new TblUserController().FetchAll().Where(x => x.UserId == int.Parse(Session["user_id"].ToString())).Single();
                        name = get_name.FullName;
                        if (get_name.ClassX != null)
                        {
                            var get_class = new TblClassController().FetchAll().Where(x => x.Id == get_name.ClassX).Single();
                            cls = get_class.ClassName;
                        }
                    }
                    if (get_test.TestExam == true)
                    {
                        if (Session["user"] == null)
                        {
                %>
                <script type="text/javascript">
                    alert("Bạn phải đăng nhập để làm bài kiểm tra");
                    window.location = "trang-chu";
                </script>
                <%
                        }
                        int get_level = 0;
                        if (Session["level"] != null)
                            get_level = int.Parse(Session["level"].ToString());
                        string[] level = new string[20];
                        if (get_test.TestLevel == "")
                        {
                %>
                <script type="text/javascript">
                    alert("Bạn không đủ quyền để truy cập!");
                    window.location = "trang-chu";
                </script>
                <%
                        }
                        else
                        {
                            level = get_test.TestLevel.Split(',');
                            int check = 0;
                            for (int i = 0; i < level.Length - 1; i++)
                            {
                                if (get_level == int.Parse(level[i]))
                                {
                                    check = 1;
                                    break;
                                }
                            }
                            if (check == 0)
                            {
                %>
                <script type="text/javascript">
                    alert("Bạn không đủ quyền để truy cập!");
                    window.location = "trang-chu";
                </script>
                <%
                            }
                        }
                    }
                    if (get_test.TestExam == true)
                    {
                        if (Session["user"] == null)
                        {
                %>
                <script type="text/javascript">
                    alert("Bạn phải đăng nhập để làm bài kiểm tra");
                    window.location = "trang-chu";
                </script>
                <%
                        }
                        else
                        {
                            var get_user = new TblUserController().FetchAll().Where(x => x.UserName.ToLower() == Session["user"].ToString()).Single();
                            int check = 0;
                            foreach (TblResult result in new TblResultController().FetchAll())
                            {
                                if (result.TestId == this.test && result.ResultUser == get_user.UserId)
                                {
                                    check = 1;
                                    break;
                                }
                            }
                            if (check == 1)
                            {
                %>
                <script type="text/javascript">
                    alert("Bạn đã hoàn thành bài kiểm tra này!");
                    window.location = "trang-chu";
                </script>
                <%
                            }
                        }
                    }

                %>
                <div class="w3-hide" id="id_test"><%=this.test %></div>
                <div class="w3-third">
                    Trình độ:&nbsp;<label><%=get_cer.CerTitle %></label>
                </div>
                <div class="w3-third">
                    Tên bài:&nbsp;<label><%=get_test.TestTitle %></label>
                </div>
                <div class="w3-third">
                    Thời gian làm bài:&nbsp;<label id="test_time"><%=get_test.TestTime %></label><label>&nbsp;Phút</label>
                </div>
                <div class="w3-third">
                    Họ tên:&nbsp;<label><%=name %></label>
                </div>
                <div class="w3-third">
                    Lớp:&nbsp;<label><%=cls %></label>
                </div>
                <div class="w3-third">
                    Thời gian còn lại:&nbsp;<label id="demnguoc1"></label>
                </div>
            </div>
            <div class="w3-row w3-teal w3-small w3-padding w3-hide-medium w3-hide-large">
                <div class="w3-half">
                    Trình độ:&nbsp;<label><%=get_cer.CerTitle %></label>
                </div>
                <div class="w3-half">
                    Tên bài:&nbsp;<label><%=get_test.TestTitle %></label>
                </div>
                <div class="w3-half">
                    Họ tên:&nbsp;<label><%=name %></label>
                </div>
                <div class="w3-half">
                    Lớp:&nbsp;<label><%=cls %></label>
                </div>
                <div class="w3-half">
                    Thời gian làm bài:&nbsp;<label><%=get_test.TestTime %>&nbsp;Phút</label>
                </div>
                <%
                    if (get_test.TestExam == true && get_test.TestStart != null)
                    {
                        if (DateTime.Parse(get_test.TestStart.ToString()) > DateTime.Now)
                        {
                %>
                <script type="text/javascript">
                    alert("chưa tới giờ làm bài");
                    window.location = "Default.aspx";
                </script>
                <%
                        }
                        else
                        {
                            if (DateTime.Parse(get_test.TestStart.ToString()).AddMinutes(int.Parse(get_test.TestTime.ToString())) < DateTime.Now)
                            {
                %>
                <script type="text/javascript">
                    alert("hết thời gian làm bài");
                    window.location = "Default.aspx";
                </script>
                <%
                            }
                            else
                            {
                                DateTime Start = DateTime.Parse(get_test.TestStart.ToString());
                                DateTime Finish = DateTime.Parse(get_test.TestStart.ToString()).AddMinutes(int.Parse(get_test.TestTime.ToString()));
                                if (DateTime.Now >= Start && DateTime.Now <= Finish)
                                {
                %>
                <div class="w3-half">
                    Thời gian còn lại:&nbsp;<label id="demnguoc2"><span class="minute_remain"><%=get_test.TestTime - (DateTime.Now.Minute - DateTime.Parse(get_test.TestStart.ToString()).Minute) %></span>
                        Phút</label>
                </div>
                <%
                                }
                            }
                        }
                    }
                    else
                    {
                %>
                <div class="w3-half">
                    Thời gian còn lại:&nbsp;<label id="demnguoc2"><span class="minute_remain"><%=get_test.TestTime %></span>
                        Phút</label>
                </div>
                <%} %>
            </div>

            <div class="task-content w3-row w3-padding w3-white">
                <header class="w3-center">
                    <label class="w3-border-bottom w3-border-teal w3-large">Bài làm</label>
                </header>
                <%
                    var get_td = new TblTestDetailController().FetchAll().Where(x => x.TestDetailActive == true).Where(c => c.TestId == this.test).OrderBy(x => Guid.NewGuid()).ToList();
                %>
                <%for (int i = 1; i <= get_td.Count; i++)
                  { %>
                <div id="<%=i %>" class="the-questions w3-row">
                    <div class="question-content w3-row w3-padding">
                        <label class="w3-border-teal" style="border-bottom: 3px solid">Câu&nbsp;<span class="thutu"><%=i %></span>:</label>&nbsp;<%=get_td[i-1].TestDetailQuestion %>
                        <%if (get_td[i - 1].TestDetailUrl != "")
                          {
                              string[] temp = new string[20];

                              temp = get_td[i - 1].TestDetailUrl.ToLower().Split('.');
                              string tail = temp[temp.Length - 1];
                              if (tail == "mp3" || tail == "wav")
                              {
                        %>
                        <p>
                            <audio src="../Content/TestData/Audio/<%=get_td[i-1].TestDetailUrl %>" controls="controls" />
                        </p>
                        <%
                              }
                              else
                              {
                        %>
                        <p>
                            <img src="../Content/TestData/Picture/<%=get_td[i-1].TestDetailUrl %>" />
                        </p>
                        <%
                              }
                          }
                        %>
                    </div>
                    <%
                          var get_ques = new TblQuestionController().FetchAll().Where(x => x.QuestionTestDetail == get_td[i - 1].TestDetailId).OrderBy(x => Guid.NewGuid()).ToList();
                          for (int j = 1; j <= get_ques.Count; j++)
                          {
                    %>
                    <div class="pa w3-row w3-padding">
                        <span class="w3-hide"><%=get_ques[j-1].QuestionTestDetail%>-<%=get_ques[j-1].QuestionId%></span><p class="pa-res w3-hide-large"><span class="<%=i%>"><%=j %></span>&nbsp;<%=get_ques[j-1].QuestionName%></p>
                        <span class="pa<%=i%> w3-hide-medium w3-hide-small"><%=j %>.&nbsp;<%=get_ques[j-1].QuestionName%></span>
                    </div>
                    <%} %>
                </div>
                <%} %>
            </div>
            <div class="w3-row w3-padding w3-center w3-teal">
                <a class="submit-btn w3-btn w3-teal w3-hover-text-teal w3-hover-white hvr-icon-buzz">Nộp bài</a>
            </div>
        </div>
        <div class="w3-col l3 w3-padding w3-hide-medium w3-hide-small">
            <div class="w3-light-grey">
                <header class="w3-padding w3-teal w3-center w3-large">Bảng trả lời</header>
                <div class="w3-white">
                <label class="w3-padding">Chọn câu trả lời:</label>
                <div class="the-answers w3-padding-left w3-padding-bottom">
                <%for (int i = 1; i <= get_td.Count; i++)
                  { %>
                <div id="c<%=i %>" class="cau w3-row">
                    <a href="#<%=i %>" class="thutu w3-btn w3-teal w3-col l2 w3-large"><%=i %></a>
                    <a class="answers w3-btn w3-light-grey w3-col l2 w3-large"><span class="hinhtron">1</span></a>
                    <a class="answers w3-btn w3-light-grey w3-col l2 w3-large"><span class="hinhtron">2</span></a>
                    <a class="answers w3-btn w3-light-grey w3-col l2 w3-large"><span class="hinhtron">3</span></a>
                    <a class="answers w3-btn w3-light-grey w3-col l2 w3-large"><span class="hinhtron">4</span></a>
                </div>
                <%} %>
                    </div>
                    </div>
            </div>
        </div>
    </div>
    <script src="../Scripts/taskjava.js"></script>
    <script src="../Scripts/multiple-choice.js"></script>
</asp:Content>
